// 选项卡
$('.tab .menus li').each(function () {
    $('.tab .menus li').click(function () {
        $('.tab .menus li').removeClass('bg');
        $(this).addClass('bg');
        var index = $(this).index();
        $('.tab .scroll').css('margin-top', -index * 492 + 'px');
    })
})

$(document).ready(function (e) {
    $(".tabnews li").click(function () {
        $(".tabnews li").eq($(this).index()).addClass("active").siblings().removeClass("active");
        $(".tabCon>div").hide().eq($(this).index()).show();
    })
});



// 分页
function page_ctrl(data_obj) {
    var obj_box = (data_obj.obj_box !== undefined) ? data_obj.obj_box : function () {
        return;
    };//翻页容器dom对象,必要参数
    var total_item = (data_obj.total_item !== undefined) ? parseInt(data_obj.total_item) : 0;//数据条目总数,默认为0,组件将不加载
    var per_num = (data_obj.per_num !== undefined) ? parseInt(data_obj.per_num) : 10;//每页显示条数,默认为10条
    var current_page = (data_obj.current_page !== undefined) ? parseInt(data_obj.current_page) : 1;//当前页,默认为1
    var total_page = Math.ceil(total_item / per_num);//计算总页数,不足2页,不加载组件
    if (total_page < 2) {
        return;
    }
    //在指定容器内加载分页数据
    $(obj_box).append('<div class="page_content"></div>');
    //在指定容器内加载分页插件
    $(obj_box).append('<div class="page_ctrl"></div>');
    function page_even() {
        /*加载数据*/
        function change_content() {
            /*此处根据项目实际自行编写页面显示内容的方法,举例说明:*/
            var page_content = '<ul  class="newslist" >';//当前页内容
            for (var i = 0; i < per_num; i++) {
                page_content += `<li>
                <div class="bgImg">
                    <img src="../images/img_bg.jpg" alt="">
                </div>
                <div class="titleIntro">
                    <div class="newsTitle">
                    `
                    + ((current_page - 1) * per_num + i + 1) + `这是标题这是标题这是标题这是标题这是标题</div>
                    <div class="newsMain">
                        这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容
                    </div>
                    <div class="dateTime">[2019/07/25]</div>
                    <div class="more">
                        <a href="newsArticle.html">更多详情&nbsp;<img src="../images/more.png" alt=""></a>
                    </div>
                </div>
            </li>`;
            }
            page_content += '</ul>';
            $(obj_box).children('.page_content').html(page_content);

        }
        change_content();

        // 显示记录

        var inp_val = (current_page == total_page) ? 1 : current_page + 1;//跳转页数,input默认显示值
        var append_html = '<div class="record_content" ><p>共</p>' + '<p>' + total_item + ' </p>' + '<p>条记录，每页</p>' + '<p>' + per_num + ' 条，页</p>' + '<p>' + current_page + '/</p>'+ '<p>' + total_page + ' </p>' + '<p>页</p></div>' + '<button class="prev_page">上一页</button>';
        for (var i = 0; i < total_page - 1; i++) {
            if (total_page > 8 && current_page > 6 && i < current_page - 3) {
                if (i < 2) {
                    append_html += '<button class="page_num">' + (i + 1) + '</button>';
                }
                else if (i == 2) {
                    append_html += '<span class="page_dot">•••</span>';
                }
            }
            else if (total_page > 8 && current_page < total_page - 3 && i > current_page + 1) {
                if (current_page > 6 && i == current_page + 2) {
                    append_html += '<span class="page_dot">•••</span>';
                } else if (current_page < 7) {
                    if (i < 8) {
                        append_html += '<button class="page_num">' + (i + 1) + '</button>';
                    } else if (i == 8) {
                        append_html += '<span class="page_dot">•••</span>';
                    }
                }
                //append_html+='<span class="page_dot">•••</span>';
            }
            else {
                if (i == current_page - 1) {
                    append_html += '<button class="page_num current_page">' + (i + 1) + '</button>';
                }
                else {
                    append_html += '<button class="page_num">' + (i + 1) + '</button>';
                }
            }
        }
        if (current_page == total_page) {
            append_html += '<button class="page_num current_page">' + (i + 1) + '</button>';
        } else {
            append_html += '<button class="page_num">' + (i + 1) + '</button>';
        }
        append_html += '<button class="next_page">下一页</button>' + '<p>共</p>' + '<span class="page_total">' + total_item + ' </span>' + '<p>条信息/</p>' + '<p>' + total_page + ' </p>' + '<p>页</p>' + '<input class="input_page_num" type="text" max="" min="" step="1" value="' + inp_val + '"><span class="page_text">页</span><button class="to_page_num">跳转</button>';

        $(obj_box).children('.page_ctrl').append(append_html);
        if (current_page == 1) {
            $(obj_box + ' .page_ctrl .prev_page').attr('disabled', 'disabled').addClass('btn_dis');
        } else {
            $(obj_box + ' .page_ctrl .prev_page').removeAttr('disabled').removeClass('btn_dis');
        }
        if (current_page == total_page) {
            $(obj_box + ' .page_ctrl .next_page').attr('disabled', 'disabled').addClass('btn_dis');
        } else {
            $(obj_box + ' .page_ctrl .next_page').removeAttr('disabled').removeClass('btn_dis');
        }

        var pageNum1 = $(".page_1").find(".page_total").html();
        var pageNum2 = $(".page_2").find(".page_total").html();
        // var pageNum2 = Math.ceil(obj_2.total_item / obj_2.per_num);

        console.log(pageNum1)
        console.log(pageNum2)

        $(".page_1").find(".input_page_num").keyup(function () {
            // alert(1)
            var c = $(this);
            if (/[^\d]/.test(c.val())) {//替换非数字字符
                var temp_amount = c.val().replace(/[^\d]/g, '');
                $(this).val(temp_amount);
            }
            console.log($(".page_1").find(".input_page_num").val());
            var v = Math.floor($(".page_1").find(".input_page_num").val(), pageNum1);
            if (v > pageNum1) {
                $(this).val(pageNum1);
            } else if (v == "") {
                $(this).val(1);
            }
            $(".page_1").find(".input_page_num").attr({
                "max": pageNum1,
                "min": 1
            });
        })
        $(".page_2").find(".input_page_num").keyup(function () {
            // alert(1)
            var c = $(this);
            if (/[^\d]/.test(c.val())) {//替换非数字字符
                var temp_amount = c.val().replace(/[^\d]/g, '');
                $(this).val(temp_amount);
            }
            var v = Math.floor($(this).val(), pageNum2);
            if (v > pageNum2) {
                $(this).val(pageNum2);
            } else if (v == "") {
                $(this).val(1);
            }
        })
        $(".page_2").find(".input_page_num").attr({
            "max": pageNum2,
            "min": 1
        });
    }
    page_even();
    $(obj_box + ' .page_ctrl').on('click', 'button', function () {
        var that = $(this);
        if (that.hasClass('prev_page')) {
            if (current_page != 1) {
                current_page--;
                that.parent('.page_ctrl').html('');
                page_even();
            }
        }
        else if (that.hasClass('next_page')) {
            if (current_page != total_page) {
                current_page++;
                that.parent('.page_ctrl').html('');
                page_even();
            }
        }
        else if (that.hasClass('page_num') && !that.hasClass('current_page')) {
            current_page = parseInt(that.html());
            that.parent('.page_ctrl').html('');
            page_even();
        }
        else if (that.hasClass('to_page_num')) {
            current_page = parseInt(that.siblings('.input_page_num').val());
            that.parent('.page_ctrl').html('');
            page_even();
        }

    });

}

// 为空跳转为1

/*容器2参数*/
var obj_1 = {
    obj_box: '.page_1',//翻页容器
    total_item: 100,//条目总数
    per_num: 8,//每页条目数
    current_page: 1//当前页
};
/*调用分页方法,初始化数据*/
page_ctrl(obj_1);

/*容器2参数*/
var obj_2 = {
    obj_box: '.page_2',//翻页容器
    total_item: 200,//条目总数
    per_num: 8,//每页条目数
    current_page: 1//当前页
};
/*调用分页方法,初始化数据*/
page_ctrl(obj_2);

